package com.taptech.doufu.util;

import android.content.Context;
import android.content.Intent;

import com.taptech.doufu.ui.activity.NewCpDesActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.bean.TagsBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by lsy on 2015/8/10.
 */
public class TagsUtil
{
    public static final int HANDLE_TYPE_TAGS2_CONTENT = 1002;
    static TagsUtil tagsUtil;

    static public TagsUtil getInstance()
    {
        if (tagsUtil == null)
        {
            tagsUtil = new TagsUtil();
        }
        return tagsUtil;
    }

    static public void enterTagSearchActivity(Context context, String tagString)
    {
        Intent intent = new Intent(context, NewCpDesActivity.class);
        intent.putExtra(Constant.KEY_SEARCH_TAG, tagString);
        intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_TAG);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    static public void enterTagDesActivity(Context context, String tag_id)
    {
        Intent intent = new Intent(context, NewCpDesActivity.class);
        intent.putExtra(Constant.TOPIC_ID, tag_id);
        intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_TAG);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

    /**
     * 打开weex的标签页面
     * @param context
     * @param tagName
     * @param sexType
     */
    public static void openWeexTagActivity(Context context,String tagName,int objectType,String sexType) {

        HashMap map = new HashMap();
        map.put("keyword", tagName);
        map.put("types", objectType);
        map.put("sexuality", sexType);
        SimpleWeexActivity.startActivity(context,"vue/tagView/TFTagView.js",map);
    }

    public static void openWeexTagActivity(Context context,String tagName,int objectType,int singlePage) {

        HashMap map = new HashMap();
        map.put("keyword", tagName);
        map.put("types", objectType);
        map.put("single_page", singlePage);
        SimpleWeexActivity.startActivity(context,"vue/tagView/TFTagView.js",map);
    }


    public static void openTagActivity(Context context,String tagName,int objectType) {

        HashMap map = new HashMap();
        map.put("keyword", tagName);
        map.put("types", objectType);
        SimpleWeexActivity.startActivity(context,"vue/tagView/TFTagView.js",map);
    }

    static public void enterTagSearchActivity(Context context, String tagString, String type)
    {
        Intent intent = new Intent(context, NewCpDesActivity.class);
        intent.putExtra(Constant.KEY_SEARCH_TAG, tagString);
        intent.putExtra(Constant.TYPE, NewCpDesActivity.TYPE_TAG);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    public synchronized void loadContentByTag2(HttpResponseListener listener, String keyWord, String last, String type)
    {
        TTLog.d("tag", "搜索标签 ============" + keyWord);
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_TYPE_TAGS2_CONTENT);
        try
        {
            request.setUrl(Constant.BASE_URL + "search/tag2?keyword=" + URLEncoder.encode(keyWord, "utf-8") + "&last=" + last + "&size=" + Constant.PAGE_SIZE_20 + "&types=" + type);
        } catch (UnsupportedEncodingException e)
        {
            request.setUrl(Constant.BASE_URL + "search/tag2?keyword=" + keyWord + "&last=" + last + "&size=" + Constant.PAGE_SIZE_20 + "&types=" + type);
        }
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public static String[] convertTagbeans2Strings(TagsBean[] tagsBeans)
    {
        String[] tagStrings = new String[tagsBeans.length];
        for (int i = 0; i < tagsBeans.length; i++)
        {
            tagStrings[i] = tagsBeans[i].getName();
        }
        return tagStrings;
    }


    /**
     * TagsBean[] 转 LinkedList<String>
     *
     * @param tagsBeans
     * @return
     */
    public static LinkedList<String> convertTagbeans2LinkedList(TagsBean[] tagsBeans)
    {
        LinkedList<String> stringLinkedList = new LinkedList<String>();
        String[] tagStrings = new String[tagsBeans.length];
        for (int i = 0; i < tagsBeans.length; i++)
        {
            tagStrings[i] = tagsBeans[i].getName();
            stringLinkedList.add(tagStrings[i]);
        }
        return stringLinkedList;
    }


}
